package com.ruoyi.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.RepeatSubmit;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.AdvPlatinfos;
import com.ruoyi.service.IAdvPlatinfosService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 *  Controller
 *  平台资讯
 * @author ruoyi
 * @date 2024-05-13
 */
@RestController
@RequestMapping("/system/platinfos")
public class AdvPlatinfosController extends BaseController
{
    @Autowired
    private IAdvPlatinfosService advPlatinfosService;

    /**
     * 查询列表
     */
    @PreAuthorize("@ss.hasPermi('system:platinfos:list')")
    @GetMapping("/list")
    public TableDataInfo list(AdvPlatinfos advPlatinfos)
    {
        startPage();
//        PageHelper.startPage(20, 10);
        List<AdvPlatinfos> list = advPlatinfosService.selectAdvPlatinfosList(advPlatinfos);
        return getDataTable(list);
    }

    @GetMapping("/list/voice/{advMembersId}/{devicesno}")
    public TableDataInfo voice(@PathVariable("advMembersId") String advMembersId,
                               @PathVariable("devicesno") String devicesno)
    {
        startPage();
        List<AdvPlatinfos> list = advPlatinfosService.selectAdvPlatinfosListVoice(advMembersId, devicesno);
        return getDataTable(list);
    }

    /**
     * 导出 列表
     */
    @PreAuthorize("@ss.hasPermi('system:platinfos:export')")
    @Log(title = " ", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AdvPlatinfos advPlatinfos)
    {
        List<AdvPlatinfos> list = advPlatinfosService.selectAdvPlatinfosList(advPlatinfos);
        ExcelUtil<AdvPlatinfos> util = new ExcelUtil<AdvPlatinfos>(AdvPlatinfos.class);
        util.exportExcel(response, list, " 数据");
    }

    /**
     * 获取 详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:platinfos:query')")
    @GetMapping(value = "/{advPlatinfosId}")
    public AjaxResult getInfo(@PathVariable("advPlatinfosId") Long advPlatinfosId)
    {
        return success(advPlatinfosService.selectAdvPlatinfosByAdvPlatinfosId(advPlatinfosId));
    }

    /**
     * 新增
     */
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('system:platinfos:add')")
    @Log(title = " ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AdvPlatinfos advPlatinfos)
    {
        String infocontent = advPlatinfos.getInfocontent();
        infocontent = parseInfoContent(infocontent);
        System.out.println("修改后的值: " + infocontent);
        advPlatinfos.setInfocontent(infocontent);
        System.out.println(advPlatinfos);
//        if (advPlatinfos.getInfoSection().equals("5")&&advPlatinfos.getDefaultAudio().equals("1")){
//            advPlatinfosService.exeUpdateDefault();
//        }
        return toAjax(advPlatinfosService.insertAdvPlatinfos(advPlatinfos));
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('system:platinfos:edit')")
    @Log(title = " ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AdvPlatinfos advPlatinfos)
    {
        String infocontent = advPlatinfos.getInfocontent();
        infocontent = parseInfoContent(infocontent);
        System.out.println("修改后的值: " + infocontent);
        advPlatinfos.setInfocontent(infocontent);
        if (advPlatinfos.getInfoSection().equals("5")){
            advPlatinfosService.exeUpdateDefault();
        }
        return toAjax(advPlatinfosService.updateAdvPlatinfos(advPlatinfos));
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('system:platinfos:remove')")
    @Log(title = " ", businessType = BusinessType.DELETE)
	@DeleteMapping("/{advPlatinfosIds}")
    public AjaxResult remove(@PathVariable Long[] advPlatinfosIds)
    {
        return toAjax(advPlatinfosService.deleteAdvPlatinfosByAdvPlatinfosIds(advPlatinfosIds));
    }

    /**
     * 设置默认视频
     */
    @PostMapping("/updateByDefault")
    public AjaxResult updateByDefault(@RequestBody AdvPlatinfos advPlatinfos)
    {
        return toAjax(advPlatinfosService.exeUpdateByDefault(advPlatinfos.getAdvPlatinfosId()));
    }

    public String parseInfoContent(String infoContent) {
        infoContent = infoContent.replaceAll("aImg", "<img style=\"width: 700rpx;height:200px;border-radius:10px;\" ");
        infoContent = infoContent.replaceAll("&gt;", ">");
        infoContent = infoContent.replaceAll("startP", "<p style=\"line-height: 1.5;\">");
        infoContent = infoContent.replaceAll("endP", "</p>");
        infoContent = infoContent.replaceAll("aTap", "&nbsp;&nbsp;&nbsp;&nbsp;");
        return infoContent;
    }

}
